/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.manage.nature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import shop.core.BaseController;
import shop.core.common.bean.JSONResult;
import shop.core.common.dao.page.PagerModel;
import shop.services.manage.nature.NatureService;
import shop.services.manage.nature.bean.Nature;

import javax.servlet.http.HttpServletRequest;

/**   
 * @类名称：NatureAction      
 * @创建人：Ltz   
 * @创建时间：2017-10-14 下午21:06:41      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/manage/nature/")
public class NatureAction extends BaseController<Nature> {
    private static final Logger logger = LoggerFactory.getLogger(NatureAction.class);
    @Autowired
    private NatureService natureService;
    private static final String page_toList = "/manage/nature/natureList";
    public NatureService getService() {
        return natureService;
    }

    private NatureAction(){
        super.page_toList = page_toList;
        super.page_toAdd = null;
        super.page_toEdit = null;
    }

    /**
     * 根据参数名称删除
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "deleteByNameJson", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult deleteByNameJson(@RequestParam(value = "name") String name) throws Exception {
        JSONResult jsonResult = new JSONResult();
        getService().deleteByName(name);
        jsonResult.setMessage("success");
        return jsonResult;
    }

    /**
     *添加商品规格
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertNatureJson", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertNatureJson(HttpServletRequest request, @ModelAttribute("nature") Nature nature) throws Exception {
        JSONResult jsonResult = new JSONResult();
        nature.setProductID(request.getParameter("productID"));      //获取商品ID
        getService().insert(nature);
        return jsonResult;
    }

    /**
     * 获取商品参数
     */
    @RequestMapping(value = "/getNature", method = RequestMethod.GET)
    @ResponseBody
    public PagerModel getSpec(HttpServletRequest request, Nature e) {
        int offset = 0;
        int pageSize = 10;
        if (request.getParameter("start") != null) {
            offset = Integer.parseInt(request.getParameter("start"));
        }
        if (request.getParameter("length") != null) {
            pageSize = Integer.parseInt(request.getParameter("length"));
        }
        if (offset < 0)
            offset = 0;
        if (pageSize < 0) {
            pageSize = 10;
        }
        e.setOffset(offset);
        e.setPageSize(pageSize);
        e.setProductID(request.getParameter("productID"));
        PagerModel pager = natureService.selectPageList(e);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }
}
